home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / edt.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  38KB  |  1,341 lines

  1. #include "all.h"
  2. #include <errno.h>
  3. #include <math.h>
  4.  
  5. #ifdef __TURBOC__
  6.         #define HLINECHAR 205
  7.         #include "bios.h"
  8.         #include "conio.h"
  9. #elif DJ                                                /* a.r. */
  10.         #define ENOFILE 999
  11.         #define HLINECHAR 205
  12.         #define clrscr ScreenClear
  13. #elif EMXOS2                                            /* a.r. */
  14.         #include <sys/video.h>
  15.         #define ENOFILE 999
  16.         #define HLINECHAR 205
  17.         extern unsigned long startPMSession(void);
  18.         extern unsigned long stopPMSession(unsigned long SessID);
  19.         unsigned long oldSession = 0;
  20.         extern int g_get_type(char *t);
  21.         int text_saveOS2tmp(void);
  22.         char devtype[60];
  23.         #ifdef SURFACE
  24.         char outputName[80];  
  25.         #endif
  26. #else
  27.         #define ENOFILE 999
  28.         #define HLINECHAR '-'
  29.         #include "vaxconio.h"
  30. #endif
  31.  
  32. #include "edt.h"
  33.  
  34. int vax_edt(char *s);
  35. int delay(int i);
  36. extern int gle_debug;
  37. int gle_speed;
  38. int gle_nspeed;
  39. int trace_on;
  40. #define true (!false)
  41. #define false 0
  42. #define dbg if (gle_debug>0)
  43. int32 mem_total(void);
  44. int pick_file(char *r, char *w);
  45. typedef char (*TEXT)[];
  46. int scr_blackwhite;
  47. int scr_left(int i);
  48. int scr_right(int i);
  49. int scr_norm(void);
  50. int scr_inv(void);
  51. int scr_grey(void);
  52. int text_search(void);
  53. int text_deleol(void);
  54. int text_undeleol(void);
  55. int scr_savexy(void);
  56. int scr_restorexy(void);
  57. int scr_gets(char *s);
  58. int text_showerror(void);
  59. int text_findnext(void);
  60. int text_changed(void);
  61. int read_command(char *ans, char *ques);
  62. int read_str(char *s);
  63. int text_load(char *fname);
  64. int text_free(void);
  65. int do_help(char *s);
  66. int int_edt(char *fname);
  67. int vinsert(int y, char *s);
  68. int vdelete(int y);
  69. int menu(void);
  70. int gle_redraw(void);
  71. int scrinsert(int y);
  72. int scrdeleteline(int y);
  73. int text_save(void) ;
  74. int text_eol(void);
  75. int text_delright(void);
  76. int text_delete(void);
  77. int text_undelete(void);
  78. int text_deleteline(void);
  79. int text_left(void);
  80. int text_right(void);
  81. int text_up(void);
  82. int text_down(void);
  83. int text_return(void);
  84. int text_refresh();
  85. int text_putstr(char *s);
  86. int text_setwindow();
  87. int text_saveas(void);
  88. int text_move(int x,int y);
  89. int scr_tab(char *s, int x);
  90. int scr_negtab(char *s, int x, int *fpos, int *scrx);
  91. int fner(char *s);
  92. int fner_clear(void);
  93. int text_setwindow(void);
  94. int text_refresh(void);
  95. int text_main(void);
  96. int text_inkey(void);
  97. int text_scroll(void);
  98. int text_scroll_up(void);
  99. int text_scroll_down(void);
  100. int text_select(void);
  101. int text_cut(void);
  102. int text_paste(void);
  103. int restofline(int y,int x);
  104. int normal_key(int c);
  105. int lineset(int i,char *ss);
  106. int lineinsert(int y, char *ss);
  107. int textinsert(int y);
  108. char *sline(int i);
  109. char *line(int i);
  110. int ncpy(char *d, char *s, int n);
  111. /*------------ GLOBAL VARIABLES --------------*/
  112. extern int32 gtotalmem;
  113. int max_ngtxt;
  114. extern int ngtxt;
  115. extern char *(*gtxt)[];   /* gtxt is a pointer to an array of poiter to char */
  116. char *(*ctxt)[];    /* cut buffer */
  117. int ncut;
  118. int iserr;
  119. int w_top=0,w_bot=0,w_len,w_cl,w_cx;
  120. #define MAXLINES (max_ngtxt-10)
  121. extern int ngtxt;
  122. int move_pos,onmove;
  123. int ii;
  124. char gbuff[255];
  125. char gbuff2[255];
  126. int selx,sely;
  127. extern char input_file[];
  128. int changed;
  129. int scr_isblackwhite(void);
  130. extern int netxt;   /* errors which occured */
  131. extern char *(*etxt)[];
  132. /*---------------------------------------------------------------------------*/
  133. /* copy gtxt, ngtxt   to local paramters */
  134. int tfx,tfy;
  135. extern int gotfile;
  136. int_edt(char *fname)
  137. {
  138.         int i,j;
  139.  
  140.         tfx = 1;
  141.         tfy = 1;
  142.  
  143. #ifdef  EMXOS2                                  /* a.r. */
  144.         w_len = 22;
  145. #else
  146.         w_len = 20;
  147. #endif
  148.         scr_blackwhite = scr_isblackwhite();
  149.         if (!gotfile) {
  150.                 { char infile[80]; char wildcard[80];
  151.         #ifdef SURFACE
  152.                 strcpy(wildcard,"*.sur");
  153.                 if (pick_file(infile,wildcard)) strcpy(infile,"test.sur");
  154.         #else
  155.                 strcpy(wildcard,"*.gle");
  156.                 if (pick_file(infile,wildcard)) strcpy(infile,"test.gle");
  157.         #endif
  158.                 text_load(infile);
  159.                 }
  160.         }
  161.         gotfile=true;
  162.         text_setwindow();
  163.         text_refresh();
  164.         text_main();
  165. }
  166. vinsert(int y,char *ss)
  167. {
  168.         int i;
  169.         if (ngtxt>(max_ngtxt-50)) text_expand(2*(ngtxt+50));
  170.         if (ngtxt>MAXLINES) {fner("too many lines in file "); return;}
  171.         ngtxt++;
  172.         for (i=ngtxt;i>y;i--)
  173.                 (*gtxt)[i] = (*gtxt)[i-1];
  174.         (*gtxt)[y] = sdup(ss);
  175. }
  176. lineinsert(int y,char *ss)
  177. {
  178.         vinsert(y,ss);
  179.         scrinsert(y);
  180. }
  181.  
  182. vdelete(int y)
  183. {
  184.         int i;
  185.         if (ngtxt==0) {fner("too few lines in file "); return;}
  186.         ngtxt--;
  187.         myfrees((*gtxt)[y],"vdelete");
  188.         for (i=y;i<=ngtxt;i++)
  189.                 (*gtxt)[i] = (*gtxt)[i+1];
  190. }
  191. linedelete(int y)
  192. {
  193.         vdelete(y);
  194.         scrdeleteline(y);
  195. }
  196. lineset(int i,char *ss)
  197. {
  198.         char *zzz;
  199.         int same;
  200.         char *d=(*gtxt)[i];
  201.         char *s=ss;
  202.         if (d==0) gle_abort("linset zero\n");
  203.         for (;*d!=0 && *s!=0 && *d==*s ;s++,d++) ;
  204.         same = s-ss;
  205.         myfrees((*gtxt)[i],"Linset");
  206.         zzz = sdup(ss);
  207.         (*gtxt)[i] = zzz;
  208.         restofline(i,same);
  209. }
  210. char *line(int i)
  211. {
  212.         static char xxx[2]="";
  213.         static char eob[7]="[EOB]";
  214.         if (i>ngtxt) {
  215.                 if (i==ngtxt+1) return &eob[0];
  216.                 else return &xxx[0];
  217.         }
  218.         return (*gtxt)[i];
  219. }
  220. text_free(void)
  221. {
  222.         int i;
  223.         for (i=1;i<=ngtxt;i++) myfrees( (*gtxt)[i] ,"textfree");
  224.         ngtxt = 0;
  225. }
  226. char *tabtospace(char *s);
  227. char *sline(int i)
  228. {
  229.         char *s;
  230.         s = tabtospace(line(i));
  231.         if (strlen(s)>77) {s[77] = '>'; s[78] = 0;}
  232.         return s;
  233. }
  234. char *tabtospace(char *s)
  235. {
  236.         static char buf[255];
  237.         char *o = &buf[0];
  238.         int p=0,k,df;
  239.         for (;*s!=0;s++) {
  240.                 if (*s==9) {
  241.                         df = (p/8)*8+8-p;
  242.                         for (k=1;k<=df;k++) {
  243.                                 *o++ = ' ';
  244.                                 p++;
  245.                         }
  246.                 } else {
  247.                         *o++ = *s;
  248.                         p++;
  249.                 }
  250.         }
  251.         *o = 0;
  252.         return &buf[0];
  253. }
  254. text_main()
  255. {
  256.         int c,doask;
  257.         for (;;) {
  258.          doask = true;
  259.          c = text_inkey();
  260.          if (iserr) fner_clear();
  261.          switch (c) {
  262.            case eexitnow:
  263.                 doask = false;
  264.            case eescape:
  265.            case equit: /* control c */
  266.                 if (text_changed()) break;
  267.                 if (doask) {
  268.                   #ifdef SURFACE
  269.                   fner("Exit from Surface ? (Y,N)  [y]");
  270.                   #else
  271.                   fner("Exit from GLE ? (Y,N)  [y]");
  272.                   #endif
  273.                   c = text_inkey();
  274.                   fner_clear();
  275.                   if (tolower(c)=='n') break;
  276.                   if (tolower(c)!='y' && c != ereturn) break;
  277.                 }
  278.                 window(1,1,80,25);
  279.                 scr_norm();
  280.                 clrscr();
  281.                 return;
  282.            case eedt: /* call edt editor */
  283.                 text_save();
  284.                 clrscr();
  285.                 fner("Entering VAX EDT");
  286.                 scr_refresh();
  287.                 vax_edt(input_file);
  288.                 text_free();
  289.                 text_load(input_file);
  290.                 text_setwindow();
  291.                 text_refresh();
  292.                 break;
  293.            case efast:
  294.                 gle_speed++;
  295.                 if (gle_speed>=gle_nspeed) gle_speed = 0;
  296.                 sprintf(gbuff,"Speed of display set to {%d}",gle_speed);
  297.                 fner(gbuff);
  298.                 break;
  299.            case eleft: /* left */
  300.                 text_left();
  301.                 break;
  302.            case eshell: /* shell, start a subprocess */
  303.                 window(1,1,80,25); scr_norm(); clrscr();
  304.                 #ifdef SURFACE
  305.                 printf("Type in EXIT to return to Surface\n\n");
  306.                 #else
  307.                 printf("Type in EXIT to return to GLE\n\n");
  308.                 #endif
  309.                 #ifndef DJ                                      /* a.r. */
  310.                 system("");
  311.                 #else
  312.                 system(getenv("COMSPEC"));
  313.                 #endif
  314.                 text_refresh();
  315.                 break;
  316.            case etrace: /* trace */
  317.                 fner("Trace is now ON (or off)");
  318.                 if (trace_on) trace_on = false; else trace_on = true;
  319.                 break;
  320.            case eright: /* right */
  321.                 text_right();
  322.                 break;
  323.            case eup: /* arrow up */
  324.                 text_up();
  325.                 break;
  326.            case edown: /* arrow down */
  327.                 text_down();
  328.                 break;
  329.            case epageup:
  330.                 tfy = tfy - 15;
  331.                 if (tfy<1) {fner("Backup past top of file"); tfy = 1;}
  332.                 text_move(tfx,tfy);
  333.                 break;
  334.            case epagedown:
  335.                 tfy = tfy + 15;
  336.                 if (tfy>ngtxt) {fner("Advance past end of file"); tfy = ngtxt;}
  337.                 text_move(tfx,tfy);
  338.                 break;
  339.            case edelright:
  340.                 changed = true;
  341.                 text_right();
  342.                 text_delete();
  343.                 break;
  344.            case edeleol:
  345.                 changed = true;
  346.                 text_deleol();
  347.                 break;
  348.            case eundeleol:
  349.                 changed = true;
  350.                 text_undeleol();
  351.                 break;
  352.            case edelline:
  353.                 changed = true;
  354.                 text_deleteline();
  355.                 break;
  356.           case edrawit:
  357.                 window(1,1,79,24);
  358.                 clrscr();
  359.                 gotoxy(1,1);
  360.                 scr_refresh();
  361.                #ifndef EMXOS2                           /* a.r. */
  362.                 gle_redraw();
  363.                 if (netxt>0) text_showerror();
  364.                 text_refresh();
  365.                #else
  366.                 g_get_type(devtype);
  367.                 if (strstr(devtype, "HARDCOPY") == NULL)
  368.                        {
  369.                         #ifdef SURFACE
  370.                         strcpy(outputName, input_file);
  371.                         #endif
  372.                         if ( oldSession != 0 ) 
  373.                                 stopPMSession(oldSession);
  374.                         text_saveOS2tmp();
  375.                         text_refresh();
  376.                         oldSession = startPMSession();
  377.                        }
  378.                 else
  379.                        {
  380.                         gle_redraw();
  381.                         if (netxt>0) text_showerror();
  382.                         text_refresh();
  383.                        }
  384.                #endif
  385.                 break;
  386.           case eshowerror:
  387.                 text_showerror();
  388.                 text_refresh();
  389.                 break;
  390.           case eundelline:
  391.                 changed = true;
  392.                 text_undelete();
  393.                 break;
  394.           case eselect:
  395.                 text_select();
  396.                 break;
  397.           case ehelp: /* help */
  398.                      #ifdef SURFACE
  399.                      do_help("SURFACE");
  400.                 #else
  401.                 do_help2("gle");
  402.                 #endif
  403.                 text_refresh();
  404.                 break;
  405.           case esaveas:
  406.                 text_saveas();
  407.                 text_refresh();
  408.                 break;
  409.           case esave: /* save file */
  410.                 text_save();
  411.                 changed = false;
  412.                 break;
  413.           case efindnext:
  414.                 text_findnext();
  415.                 break;
  416.           case esearch: /* search for string */
  417.                 text_search();
  418.                 break;
  419.           case egraphmenu: /* graph mode  */
  420.                 changed = true;
  421.                 menu();
  422.                 text_refresh();
  423.                 break;
  424.           case eload:
  425.                 { char infile[80]; char wildcard[80];
  426.                 if (text_changed()) break;
  427.                 if (read_command(infile,"Name of file to load <return for menu> ")) break;
  428.                 strcpy(wildcard,infile);
  429.                 #ifdef SURFACE
  430.                 if (strlen(infile)==0) strcpy(wildcard,"*.sur");
  431.                 #else
  432.                 if (strlen(infile)==0) strcpy(wildcard,"*.gle");
  433.                 #endif
  434.                 if (strchr(wildcard,'*')!=NULL)
  435.                         if (pick_file(infile,wildcard)) goto dontload;
  436.                 text_free();
  437.                 text_load(infile);
  438.                 }
  439.                 text_setwindow();
  440. dontload:   text_refresh();
  441.                 break;
  442.           case ecut: /* control K , delete line, or block */
  443.                 changed = true;
  444.                 text_cut();
  445.                 break;
  446.           case emark: /* quick hack a.r. */
  447.                 changed = true;
  448.                 text_cut();
  449.                 text_paste();
  450.                 break;
  451.           case ebol:
  452.                 text_move(1,tfy);
  453.                 break;
  454.           case eeol:
  455.                 text_move(strlen(line(tfy))+1,tfy);
  456.                 break;
  457.           case eendoffile:
  458.                 text_move(strlen(line(ngtxt))+1,ngtxt);
  459.                 break;
  460.           case etopoffile:
  461.                 text_move(1,1);
  462.                 break;
  463.           case epaste: /* control Y , Yank line back, or block */
  464.                 changed = true;
  465.                 text_paste();
  466.                 break;
  467.           case ereturn: /* carriage return */
  468.                 changed = true;
  469.                 text_return();
  470.                 break;
  471.           case edelete: /* delete */
  472.                 changed = true;
  473.                 text_delete();
  474.                 break;
  475.           default: /* normal key */
  476.                 changed = true;
  477.                 onmove = false;
  478.                 if (c<26  && c!=9) {fner("Key has no affect"); text_move(tfx,tfy); break;}
  479.                 if (c>200)  {fner("Unimplemented command"); text_move(tfx,tfy);}
  480.                 else normal_key(c);
  481.                 break;
  482.          }
  483.         }
  484. }
  485. text_changed()
  486. {
  487.         int c;
  488.         if (!changed) return false;
  489.         for (;;) {
  490.                 fner("Save changes ? (Y,N)");
  491.                 c = text_inkey();
  492.                 fner_clear();
  493.                 if (c==eescape) return true;
  494.                 if (tolower(c)=='n') return false;
  495.                 if (tolower(c)=='y') {text_save(); return false;}
  496.         }
  497. }
  498. int text_expand(int n);
  499. text_expand(int n)
  500. {
  501.         char **a;
  502.         max_ngtxt = n;
  503.         a = myallocz((n+1)*sizeof(char *));
  504.         if (gtxt!=NULL) {
  505.                 memcpy(a,gtxt,(ngtxt+1)*sizeof(char *));
  506.                 myfrees(gtxt,"textexpand");
  507.         }
  508.         gtxt = (char *(*)[]) a;
  509. }
  510. text_load(char *fname)
  511. {
  512. static char inbuff[200];
  513. FILE *fptr;
  514. char *s;
  515. int i;
  516. char emess[80];
  517.         changed = false;
  518.         strcpy(input_file,fname);
  519.         s = strchr(input_file,'.');
  520.         #ifdef SURFACE
  521.         if (s==NULL) strcat(input_file,".sur");
  522.         #else        
  523.         if (s==NULL) strcat(input_file,".gle");
  524.         #endif
  525.         fptr = fopen(input_file,"r");
  526.         if (fptr==NULL) 
  527.            {
  528.                 if (errno == ENOFILE) 
  529.                   {
  530.                    sprintf(emess,"Input file does not exist (%s) ",input_file);
  531.                    fner(emess);
  532.                    delay(1000);
  533.                   }
  534.                 else
  535.                       {
  536.                        #if (defined EMXOS2 && defined SURFACE)
  537.                         fprintf(stderr, "Unable to open input file {%s} \n",input_file);
  538.                        #else
  539.                         gprint("Unable to open input file {%s} \n",input_file);
  540.                        #endif
  541.                         perror("Reason");
  542.                         fner("Press return to continue");
  543.                         text_inkey();
  544.                       }
  545.            }
  546.         else 
  547.            {
  548.             for (;!feof(fptr);)
  549.                {
  550.                 if (fgets(inbuff,200,fptr)!=NULL)
  551.                    {
  552.                         i = strlen(inbuff);
  553.                         if (inbuff[i-1]=='\n') inbuff[i-1] = 0;
  554.                         ngtxt++;
  555.                         if (ngtxt>(max_ngtxt-300)) {
  556.                                 text_expand(2*(ngtxt+500));
  557.                         }
  558.                         (*gtxt)[ngtxt] = myalloc(i+1);
  559.                         strcpy((*gtxt)[ngtxt],inbuff);
  560.                    }
  561.                }
  562.          fclose(fptr);
  563.         }
  564.         #ifdef SURFACE
  565.         if (ngtxt==0) 
  566.            {
  567.                 (*gtxt)[++ngtxt] = sdup("begin surface");
  568.                 (*gtxt)[++ngtxt] = sdup("   data test.z");
  569.                 (*gtxt)[++ngtxt] = sdup("end surface");
  570.            }
  571.         #else
  572.         if (ngtxt==0) (*gtxt)[++ngtxt] = sdup("size 24 18");
  573.         #endif
  574.         if (ngtxt==1) (*gtxt)[++ngtxt] = sdup("");
  575.         tfx = 1;
  576.         tfy = 1;
  577. }
  578. char errgle[90];
  579. int dont_clear;
  580. gle_abort(char *s)
  581. {
  582.         printf("%s",s); scr_refresh();
  583.         d_tidyup();
  584.         delay(1000);
  585.         dont_clear = true;
  586.         window(1,1,80,25);
  587.         scr_norm();
  588.         clrscr();
  589.         #ifdef SURFACE
  590.         printf("\n\n\n\n %s \nSurface is aborting, Attempting to save file in abort.gle\n",s);
  591.         #else
  592.         printf("\n\n\n\n %s \nGLE is aborting, Attempting to save file in abort.gle\n",s);
  593.         #endif
  594.         strcpy(input_file,"abort.gle");
  595.         text_save();
  596.         printf("File is now saved (hopefully)\n");
  597.         if (strstr(s,"alloc")!=NULL  || strstr(s,"Init")!=NULL) {
  598.                    #ifdef SURFACE
  599.                 printf("\nSurface is short of memory!!\n\n");
  600.                 #else
  601.                 printf("\nGLE is short of memory!!\n\n");
  602.                 #endif
  603.                 printf("If you are using very large datasets you should try\n");
  604.                 printf("the BIGFILE option, otherwise you probably have too\n");
  605.                 printf("many memory resident programs on your pc and to make GLE work\n");
  606.                 printf("you should remove these from your CONFIG.SYS and AUTOEXEC.BAT\n");
  607.                 printf("\n  (make copies of these files before altering them)\n");
  608.         }
  609.         scr_refresh();
  610.         scr_end();
  611.         exit(1);
  612. }
  613. text_save()
  614. {
  615. static int tver=0;
  616. char tbuf[15];
  617. FILE *fptr;
  618. int i;
  619.         changed = false;
  620.         fner("Saving file");
  621. #if (defined __TURBOC__ || defined DJ)
  622.         if (input_file[1] == ':') setdisk(toupper(input_file[0])-'A');
  623. #endif
  624. #ifdef EMXOS2                                           /* a.r. */
  625.         if (input_file[1] == ':')
  626.                 DosSetDefaultDisk(toupper(input_file[0])-'A' + 1);
  627. #endif
  628.         tver++; if (tver==5) tver=1;
  629.         sprintf(tbuf,"gletmp.t%d",tver);
  630.         unlink(tbuf);
  631.         unlink("glegle.tmp");
  632. #ifdef VAXC
  633. #include <file.h>
  634. {
  635. int file_desc;
  636.         file_desc = open("glegle.tmp",O_WRONLY | O_CREAT | O_TRUNC ,0,"rat=cr","rfm=var");
  637.         if (file_desc==-1) { gprint("Open glegle.tmp error \n");  return;}
  638.         fptr = fdopen(file_desc,"w");
  639. }
  640. #else
  641.         fptr = fopen("glegle.tmp","w");
  642. #endif
  643.         if (fptr==NULL) perror("Unable to open output file GLEGLE.TMP");
  644.         for (i=1;i<=ngtxt;i++) {
  645.                 if (fputs(line(i),fptr)==EOF
  646.                         && i<ngtxt &&  (strlen(line(i)) > 0) )
  647.                         printf("Unable to write line %d {%s} \n",i,line(i));
  648.                 fputs("\n",fptr);
  649.         }
  650.         if (fclose(fptr)!=0) perror("Unable to close output file GLEGLE.TMP");
  651. #ifndef VMS
  652.         if (rename(input_file,tbuf)!=0) ; /*
  653.                 perror("Unable to rename input file to gletmp.t*"); */
  654. #endif
  655. #ifdef EMXOS2                                   /* a.r. */
  656. #ifdef SURFACE
  657.         unlink(input_file);
  658. #endif
  659. #endif
  660.         if (rename("glegle.tmp",input_file)!=0)
  661.            perror("Unable to rename GLEGLE.TMP to output file");
  662.         fner_clear();
  663. }
  664. text_bol()
  665. {
  666.         tfx = 1;
  667.         /*        text_move(tfx,tfy);         in surf !! */
  668. }
  669. text_eol()
  670. {
  671.         tfx = strlen(line(tfy))+1;
  672.         text_move(tfx,tfy);
  673. }
  674. text_left()
  675. {
  676.         onmove = false;
  677.         if (tfx==1) {
  678.                 if (tfy==1) return;
  679.                 tfy--; tfx = strlen(line(tfy))+1;
  680.         } else tfx--;
  681.         text_move(tfx,tfy);
  682. }
  683. text_right()
  684. {
  685.         onmove = false;
  686.         if (tfx== 1+strlen(line(tfy)) ) {
  687.                 if (tfy==ngtxt) return;
  688.                 tfy++; tfx = 1;
  689.         } else tfx++;
  690.         text_move(tfx,tfy);
  691. }
  692. text_up()
  693. {
  694.         if (tfy==1) {fner("Backup past end of file"); text_move(tfx,tfy); return;}
  695.         if (!onmove) {
  696.                 onmove = true;
  697.                 move_pos = scr_tab(line(tfy),tfx);
  698.         }
  699.         tfy--;
  700.         scr_negtab(line(tfy),move_pos,&tfx,&ii);
  701.         text_move(tfx,tfy);
  702. }
  703. text_down()
  704. {
  705.         if (tfy==ngtxt) {fner("Advance past end of file"); tfy--;}
  706.         if (!onmove) {
  707.                 onmove = true;
  708.                 move_pos = scr_tab(line(tfy),tfx);
  709.         }
  710.         tfy++;
  711.         scr_negtab(line(tfy),move_pos,&tfx,&ii);
  712.  /*gotoxy(5,9);gprint("negtab line=%d m=%d f=%d \n",tfy,move_pos,tfx);*/
  713.         text_move(tfx,tfy);
  714. }
  715. text_return()
  716. {
  717.         strcpy(gbuff,line(tfy)+tfx-1);
  718.         ncpy(gbuff2,line(tfy),tfx-1);
  719.         lineset(tfy,gbuff2);
  720.         lineinsert(tfy+1,gbuff);
  721.         text_right();
  722. }
  723. char line_buff[255];
  724. text_undelete()
  725. {
  726.         lineinsert(tfy,line_buff);
  727.         text_move(tfx,tfy);
  728. }
  729. text_undeleol()
  730. {
  731.         strcpy(gbuff2,line(tfy)+tfx-1);
  732.         ncpy(gbuff,line(tfy),tfx-1);
  733.         strcat(gbuff,line_buff);
  734.         lineinsert(tfy,gbuff);
  735.         lineset(tfy+1,gbuff2);
  736.         text_move(tfx,tfy);
  737. }
  738. text_saveas()
  739. {
  740.         char *s;
  741.         #ifdef SURFACE
  742.         if (read_command(input_file,"New Surface file name ")) return;
  743.         #else
  744.         if (read_command(input_file,"New GLE file name ")) return;
  745.         #endif
  746.         s = strchr(input_file,'.');
  747.         #ifdef SURFACE
  748.         if (s==NULL) strcat(input_file,".sur");
  749.         #else
  750.         if (s==NULL) strcat(input_file,".gle");
  751.         #endif
  752.         text_save();
  753. }
  754. #ifdef EMXOS2
  755. text_saveOS2tmp()                                       /* a.r. */
  756. {
  757.            #ifdef SURFACE
  758.                        #define TEMPFILENAME "surfos2.tmp"
  759.            #else
  760.                        #define TEMPFILENAME "gleos2.tmp"
  761.            #endif
  762.  
  763.         FILE *fptr;
  764.         int i;
  765.  
  766.         unlink(TEMPFILENAME);
  767.         fptr = fopen(TEMPFILENAME,"w");
  768.         if (fptr==NULL) perror("Unable to open output file GLEOS2.TMP");
  769.         for (i=1;i<=ngtxt;i++) {
  770.                 if (fputs(line(i),fptr)==EOF
  771.                         && i<ngtxt &&  (strlen(line(i)) > 0) )
  772.                         printf("Unable to write line %d {%s} \n",i,line(i));
  773.                 fputs("\n",fptr);
  774.         }
  775.         if (fclose(fptr)!=0) perror("Unable to close output file GLEOS2.TMP");
  776. }
  777. #endif
  778.  
  779. char search_string[100];
  780. text_search()
  781. {
  782.         if (read_command(search_string,"Enter string to search for ")) return;
  783.         strlwr(search_string);
  784.         text_findnext();
  785. }
  786. text_findnext()
  787. {
  788.         int i;
  789.         char fline[100];
  790.         char *p;
  791.         for (i=tfy+1; i<=ngtxt; i++) {
  792.                 ncpy(fline,line(i),90);
  793.                 strlwr(fline);
  794.                 p = strstr(fline,search_string);
  795.                 if (p!=NULL) {
  796.                         tfy = i; tfx = p - &fline[0] + 1;
  797.                         text_move(tfx,tfy);
  798.                         return;
  799.                 }
  800.         }
  801.         fner("String was not found");
  802. }
  803. text_select()
  804. {
  805.         selx = tfx; sely = tfy;
  806.         text_move(tfx,tfy);
  807. }
  808. int swapint(int *a,int *b);
  809. swapint(int *a, int *b)
  810. {
  811.         int c;
  812.         c = *a; *a = *b; *b = c;
  813. }
  814. text_cut()
  815. {
  816.         int i,x1=selx,y1=sely,x2=tfx,y2=tfy;
  817.         if (selx==0 && sely==0) return;
  818.         if (y1==y2)  if (x2<x1) swapint(&x1,&x2);
  819.         if (y2<y1) { swapint(&x1,&x2); swapint(&y1,&y2); }
  820.         if (ncut>0) {
  821.                 myfrees(ctxt,"textcut");
  822.                 for (i=1;i<=ncut;i++) myfree((*ctxt)[i]);
  823.         }
  824.         ctxt = myalloc((y2-y1+3)*sizeof(char *));
  825.         ncut = 1;
  826.         if (y1==y2) {
  827.                 ncpy(gbuff,line(y1)+x1-1,x2-x1);
  828.                 (*ctxt)[1] = sdup(gbuff);
  829.                 ncpy(gbuff,line(y1),x1-1);
  830.                 strcat(gbuff,line(y1)+x2-1);
  831.                 lineset(y1,gbuff);
  832.                 text_move(x1,y1);
  833.                 return;
  834.         }
  835.         for (i=y1+1;i<y2;i++) (*ctxt)[++ncut] = sdup(line(i));
  836.         for (i=y1+1;i<y2;i++) linedelete(y1+1);
  837.         (*ctxt)[1] = sdup(line(y1)+x1-1);
  838.         ncpy(gbuff2,line(y1+1),x2-1);
  839.         (*ctxt)[++ncut] = sdup(gbuff2);
  840.         ncpy(gbuff,line(y1),x1-1);
  841.         strcat(gbuff,line(y1+1)+x2-1);
  842.         lineset(y1,gbuff);
  843.         linedelete(y1+1);
  844.         text_move(x1,y1);
  845. }
  846. text_paste()
  847. {
  848.         int i;
  849.         if (ncut==0) return;
  850.         if (ncut==1) {
  851.                 ncpy(gbuff,line(tfy),tfx-1);
  852.                 strcat(gbuff,(*ctxt)[1]);
  853.                 strcat(gbuff,line(tfy)+tfx-1);
  854.                 lineset(tfy,gbuff);
  855.                 text_move(tfx+strlen((*ctxt)[1]),tfy);
  856.                 return;
  857.         }
  858.         ncpy(gbuff,line(tfy),tfx-1);
  859.         strcat(gbuff,(*ctxt)[1]);
  860.         strcpy(gbuff2,(*ctxt)[ncut]);
  861.         strcat(gbuff2,line(tfy)+tfx-1);
  862.         lineset(tfy,gbuff);
  863.         lineinsert(tfy+1,gbuff2);
  864.         for (i=ncut-1;i>1;i--) lineinsert(tfy+1, (*ctxt)[i]);
  865.         text_move(strlen((*ctxt)[ncut])+1,tfy+ncut-1);
  866. }
  867. text_deleol()
  868. {
  869.         if (ngtxt+1==tfy) return;
  870.         strcpy(line_buff,line(tfy)+tfx-1);
  871.         ncpy(gbuff,line(tfy),tfx-1);
  872.         if (tfy<ngtxt) strcat(gbuff,line(tfy+1));
  873.         lineset(tfy,gbuff);
  874.         if (ngtxt<=tfy) {
  875.                 gbuff[0] = 0;
  876.                 lineinsert(tfy+1,gbuff);
  877.         } else  linedelete(tfy+1);
  878.         text_move(tfx,tfy);
  879. }
  880. text_deleteline()
  881. {
  882.         if (ngtxt==1) return;
  883.         strcpy(line_buff,line(tfy));
  884.         linedelete(tfy);
  885.         if (ngtxt<tfy) {
  886.                 gbuff[0] = 0;
  887.                 lineinsert(tfy,gbuff);
  888.         }
  889.         text_move(tfx,tfy);
  890. }
  891. text_delete()
  892. {
  893.         int oldx;
  894.         if (tfx==1) {
  895.                 if (tfy==1) {fner("Backup past top of file"); return;}
  896.                 text_left();
  897.                 oldx = tfx;
  898.                 strcpy(gbuff2,line(tfy));
  899.                 text_deleteline();
  900.                 strcat(gbuff2,line(tfy));
  901.                 lineset(tfy,gbuff2);
  902.                 tfx = oldx;
  903.                 text_move(tfx,tfy);
  904.                 return;
  905.         }
  906.         ncpy(gbuff,line(tfy),tfx-2);
  907.         strcat(gbuff,line(tfy)+tfx-1);
  908.         tfx--;
  909.         lineset(tfy,gbuff);
  910.         text_move(tfx,tfy);
  911. }
  912. text_setwindow()
  913. {
  914.         if (tfy>w_top-4 || tfy<w_bot+4) w_top = tfy - (w_len-1)/2;
  915.         if (w_top<1) w_top = 1;
  916.         w_bot = w_top + w_len - 1;
  917. }
  918. text_refresh()
  919. {
  920.         char doubleline[90];
  921.         int e,i;
  922.         iserr = true; /* so that function keys appear at bottom */
  923.         window(1,1,80,25);
  924.         scr_norm();
  925.         clrscr();
  926.         #ifdef SURFACE
  927.         gotoxy(1,1); wprintf("Memory Used=%ld  Free=%ld   Surface V3.3h Current file={%s}"
  928.                 ,mem_total(),coreleft(),input_file);
  929.         #else
  930.         gotoxy(1,1); wprintf("Memory Used=%ld  Free=%ld   GLE V3.3h  Current file={%s}"
  931.                 ,mem_total(),coreleft(),input_file);
  932.         #endif
  933.  
  934.         memset(doubleline,HLINECHAR,80);
  935.         doubleline[78] = 0;
  936.         gotoxy(1,2); cputs(doubleline);
  937.  
  938.         fner_clear();
  939.  
  940.  
  941.        #if ( defined DJ || defined EMXOS2 )                     /* a.r. */
  942.         window(2,3,79,24);
  943.        #else
  944.         window(2,3,79,23);
  945.        #endif
  946.  
  947.         gotoxy(1,1);
  948.         e = w_bot;
  949.         if (e>ngtxt) e = ngtxt;
  950.         for (i=w_top;i<=e;i++) {
  951.                 gotoxy(1,i-w_top+1);
  952.                 cputs( sline(i) );
  953.         }
  954.         if (e<w_bot) {
  955.                 gotoxy(1,i-w_top+1);
  956.                 cputs("[EOF]");
  957.         }
  958.         gotoxy(scr_tab(line(tfy),tfx), tfy - w_top + 1);
  959. }
  960. /*---------- move cursor to file coloumn x, line y */
  961. text_move(int x,int y)
  962. {
  963.         tfx = x;
  964.         tfy = y;
  965.         if (tfy>ngtxt) tfy = ngtxt;
  966.         if (tfy<1) tfy = 1;
  967. /*  if (tfx>79) tfx = 79; */
  968.         if (tfx>strlen(line(tfy))+1) tfx = strlen(line(tfy))+1;
  969.         if (tfx<1) tfx = 1;
  970.         if (tfy<(w_top+5) || tfy>(w_bot-5)) {
  971.                 if (tfy<(w_top-25) || tfy>(w_bot+25)) {
  972.                         text_setwindow();
  973.                         text_refresh();
  974.                 } else {
  975.                         text_scroll();
  976.                 }
  977.         }
  978.         gotoxy(scr_tab(line(tfy),tfx), tfy - w_top + 1);
  979. }
  980. restofline(int y,int x)
  981. {
  982.         int sx;
  983.         if (x <= strlen(line(y))) {
  984.                 sx = scr_tab(line(y),x);
  985.                 gotoxy(sx,y-w_top + 1);
  986.                 clreol();
  987.                 cputs(sline(y)+sx-1);
  988.                 gotoxy(scr_tab(line(y),tfx), y - w_top + 1);
  989.         }
  990. }
  991. fixline(int y)
  992. {
  993.         gotoxy(1,y-w_top + 1);
  994.         clreol();
  995.         cputs(sline(y));
  996. }
  997. scrinsert(int y)
  998. {
  999. #ifndef EMXOS2                                  /* a.r. */
  1000. #ifdef DJ
  1001.         if (y-w_top + 1 >= w_len ) 
  1002.                {
  1003.                 text_refresh();
  1004.                 return;
  1005.                }
  1006. #endif
  1007.         gotoxy(1,y-w_top + 1);
  1008.         insline();
  1009.         cputs(sline(y)); clreol();
  1010. #else
  1011.         v_scroll( 0, y - w_top + 2, 79, 23, 1, V_SCROLL_DOWN );
  1012.         gotoxy(1, y-w_top);      clreol();
  1013.         gotoxy(1, y-w_top);      cputs(sline(y-1));
  1014.         gotoxy(1, y-w_top + 1);  cputs(sline(y));
  1015. #endif
  1016. #ifdef DJ                                       /* a.r. */
  1017.         gotoxy(1,y-w_top + 2);
  1018.         cputs(sline(y+1));
  1019.         gotoxy(1,y-w_top + 1);
  1020. #endif
  1021. }
  1022. text_scroll()
  1023. {
  1024.         int n,i;
  1025.         n = w_top - tfy + 5;
  1026.         /* printf("n %d, w_top %d  tfy %d \n",n,w_top,tfy); */
  1027.         if (w_top-n<1) n = w_top - 1;
  1028.         if (n>0) {
  1029.                 for (i=0;i<n;i++) text_scroll_down();
  1030.                 return;
  1031.         }
  1032.         n = tfy - w_bot + 5;
  1033.         if (w_bot+n>ngtxt) n = ngtxt - w_bot;
  1034.         if (n>0) {
  1035.                 for (i=0;i<n;i++) text_scroll_up();
  1036.                 return;
  1037.         }
  1038. }
  1039. /* ------------------------ Find screen position given x pos in string */
  1040. scr_tab(char *s, int x)
  1041. {
  1042.         int p=0,i=0;
  1043.         for (;*s!=0 && i<(x-1);i++,s++) {
  1044.                 if (*s==9)  p = (p/8)*8+8;
  1045.                 else       p++;
  1046.         }
  1047.         if (p>= 77) p = 77;
  1048.         return ++p;
  1049. }
  1050. /*------------------------- Find file x position given x pos on screen */
  1051. /* and sets scrx to the correct screen position (if in middle of tab) */
  1052. scr_negtab(char *s, int x, int *fpos, int *scrx)
  1053. {
  1054.         int last_pos=0,i;
  1055.         *fpos=0;
  1056.         *scrx = 0;
  1057.  
  1058.         for (i=0;*s!=0;s++,i++) {
  1059.                 if (*s==9) *scrx = (*scrx/8)*8+8;
  1060.                 else (*scrx)++;
  1061.                 if ((*scrx+1)>x) {
  1062.                         *fpos = i+1;
  1063.                         *scrx = last_pos;
  1064.                         return;
  1065.                 }
  1066.                 last_pos = *scrx + 1;
  1067.         }
  1068.         if (*scrx==0) (*scrx)++;
  1069.         *fpos = i+1;
  1070.         if (i==0) *fpos = 1;
  1071. }
  1072. text_scroll_down()
  1073. {
  1074. #ifndef EMXOS2                                          /* a.r. */
  1075.  #ifdef __TURBOC__
  1076.         window(2,3,79,22);
  1077.  #endif
  1078.  #ifdef DJ                                               /* a.r. */
  1079.         window(2,3,79,24);
  1080.  #endif
  1081.  
  1082.         gotoxy(1,1);insline();
  1083.  
  1084.  #ifdef __TURBOC__
  1085.         window(2,3,79,23);
  1086.  #endif
  1087. #else
  1088.         #ifdef SURFACE        /* really neccessary ? */
  1089.         window(2,3,79,24);
  1090.         #endif
  1091.         v_scroll(0,2,79,23,1,V_SCROLL_DOWN);
  1092. #endif
  1093.         w_top--;w_bot--;
  1094.         fixline(w_top);
  1095. #ifdef DJ                       /* a.r. insline is not really well implemented */
  1096.         fixline(w_top+1);
  1097. #endif
  1098. }
  1099. text_scroll_up()
  1100. {
  1101. #ifndef EMXOS2                                          /* a.r. */
  1102.         gotoxy(1,1); delline();
  1103. #else
  1104.         v_scroll(0,2,79,23,1,V_SCROLL_UP);
  1105. #endif
  1106.         w_top++; w_bot++;
  1107.         fixline(w_bot);
  1108. }
  1109. scrdeleteline(int y)
  1110. {
  1111. #ifndef EMXOS2                                          /* a.r. */
  1112.         gotoxy(1, y - w_top + 1);
  1113.         delline();
  1114.         fixline(w_bot);
  1115. #else
  1116.         v_scroll( 0, y - w_top + 2, 79, 23, 1, V_SCROLL_UP );
  1117.         fixline(w_bot);
  1118. #endif
  1119. }
  1120. extern int noscreenio;
  1121. char *function_bar(void);
  1122. fner_clear()
  1123. {
  1124.         if (noscreenio) return;
  1125.         if (iserr==false) return;
  1126.         if (dont_clear) return;
  1127.         window(1,1,80,25);
  1128.         gotoxy(1,25);
  1129.         scr_grey();
  1130.         clreol();
  1131.         gotoxy(2,25);
  1132.         cputs(function_bar());
  1133.         scr_norm();
  1134.         iserr = false;
  1135. #if (defined DJ || defined EMXOS2)                      /* a.r. */
  1136.         window(2,3,79,24);
  1137. #else
  1138.         window(2,3,79,23);
  1139. #endif
  1140. }
  1141. fner(char *s)
  1142. {
  1143.         if (dont_clear) return;
  1144.         if (noscreenio) {
  1145.                 printf("%s\n",s);
  1146.                 return;
  1147.         }
  1148.         scr_savexy();
  1149.         iserr = true;
  1150.         window(1,1,80,25);
  1151.         gotoxy(1,25);
  1152.         scr_inv();
  1153.         clreol();
  1154.         gotoxy(2,25);
  1155.         cputs(s);
  1156.         scr_norm();
  1157.         scr_restorexy();
  1158.         scr_refresh();
  1159. }
  1160. int read_command(char *ans,char *ques)
  1161. {
  1162.         int rr;
  1163.         iserr = true;
  1164.         window(1,1,80,25);
  1165.         gotoxy(1,24);
  1166.         scr_inv();
  1167.         clreol();
  1168.         gotoxy(2,24);
  1169.         cputs(ques);
  1170.         rr = read_str(ans);
  1171.         gotoxy(1,24);
  1172.         scr_norm();
  1173.         clreol();
  1174. #if (defined DJ || defined EMXOS2)                      /* a.r. */
  1175.         window(2,3,79,24);
  1176. #else
  1177.         window(2,3,79,23);
  1178. #endif
  1179.         text_move(tfx,tfy);
  1180.         return rr;
  1181. }
  1182. int read_input(char *ans,char *ques)
  1183. {
  1184.         int rr;
  1185.         iserr = true;
  1186.         clreol();
  1187.         cputs(ques);
  1188.         rr = read_str(ans);
  1189.         #ifdef DJ                              /* a.r. */
  1190.         wprintf("\n\r        ");
  1191.         #else
  1192.         wprintf("\n");
  1193.         #endif
  1194.         return rr;
  1195. }
  1196.  
  1197. int read_str(char *s)
  1198. {
  1199.         int c,cx=0;
  1200.         char mbuff[80];
  1201.         *s = 0;
  1202.         for (;;) {
  1203.          c = text_inkey();
  1204.          switch (c) {
  1205.            case eescape: /* ESCAPE */
  1206.            case equit: /* control c */
  1207.                 return true;
  1208.            case epageup:
  1209.                 return epageup;
  1210.            case epagedown:
  1211.                 return epagedown;
  1212.            case eleft: /* left */
  1213.                 if (cx <= 0) break;
  1214.                 cx--;
  1215.                 scr_left(1);
  1216.                 break;
  1217.            case eright: /* right */
  1218.                 if (cx >= strlen(s)) break;
  1219.                 cx++;
  1220.                 scr_right(1);
  1221.                 break;
  1222.            case eup: /* arrow up */
  1223.            case edown: /* arrow down */
  1224.           case ehelp: /* f1 help */
  1225.                 break;
  1226.           case ereturn: /* carriage return */
  1227.                 return false;
  1228.           case edelete: /* delete */
  1229.                 if (strlen(s)==0) break;
  1230.                 if (cx<1) break;
  1231.                 ncpy(mbuff,s,cx-1);
  1232.                 strcat(mbuff,s + cx);
  1233.                 strcpy(s,mbuff);
  1234.                 cx--;
  1235.                 scr_left(1);
  1236.                 cputs(s + cx);
  1237.                 putch(' ');
  1238.                 scr_left(strlen(s+cx)+1);
  1239.                 break;
  1240.           case eshowerror:
  1241.           case edrawit:
  1242.                 break;
  1243.           default: /* normal key */
  1244.                 if (c<26  && c!=9) {fner("Key has no affect"); break;}
  1245.                 if (c>200)  fner("Unimplemented command");
  1246.                 else {
  1247.                         ncpy(mbuff,s,cx);
  1248.                         mbuff[cx] = c; mbuff[cx+1] = 0;
  1249.                         strcat(mbuff,s + cx);
  1250.                         strcpy(s,mbuff);
  1251.                         cputs(s + cx);
  1252.                         cx++;
  1253.                         scr_left(strlen(s+cx));
  1254.                 }
  1255.                 break;
  1256.            }
  1257.          }
  1258. }
  1259. normal_key(int c)
  1260. {
  1261.         static char lbuff[255];
  1262.         if (ngtxt+1==tfy) {
  1263.                 text_up();
  1264.                 text_eol();
  1265.                 text_return();
  1266.         }
  1267.         ncpy(lbuff,line(tfy),tfx-1);
  1268.         lbuff[tfx-1] = c; lbuff[tfx]=0;
  1269.         strcat(lbuff,line(tfy)+tfx-1);
  1270.         tfx++;
  1271.         lineset(tfy,lbuff);
  1272. }
  1273. text_showerror()
  1274. {
  1275.         int i,n;
  1276.  
  1277.        #ifdef EMXOS2                            /* a.r. */
  1278.         FILE *fptr;
  1279.         int j;
  1280.         char buffer[256];
  1281.  
  1282.         g_get_type(devtype);
  1283.         if (strstr(devtype, "HARDCOPY") == NULL)
  1284.                {
  1285.                 clrscr();
  1286.                 fptr = fopen("GLEOS2.ERR","r");
  1287.                 if (fptr == NULL) 
  1288.                        {
  1289.                         fner("There were no errors. Press any key to continue...");
  1290.                         text_inkey();
  1291.                         return;
  1292.                        }
  1293.                 else
  1294.                          for (j = 0;!feof(fptr); j++)
  1295.                                {
  1296.                                 if (fgets(buffer,80,fptr)!=NULL)
  1297.                                        {
  1298.                                         v_gotoxy(0,j);
  1299.                                         v_puts(buffer);
  1300.                                        }
  1301.                                }
  1302.                 fclose(fptr);
  1303.                }
  1304.                 fner("Press any key to continue...");
  1305.                 text_inkey();
  1306.                 return;
  1307.        #endif                   /* EMXOS2 */
  1308.         if (netxt==0) {
  1309.                 fner("There were no errors, press return");
  1310.                 text_inkey();
  1311.                 return;
  1312.         }
  1313.         clrscr();
  1314.         n = netxt;
  1315.         if (n>20) n = 17;
  1316.         for (i=1;i<=n;i++) {
  1317.                 gotoxy(3,i);
  1318.                 cputs( (*etxt)[i] );
  1319.         }
  1320.         gotoxy(1,20); cputs("Press any key to continue");
  1321.         text_inkey();
  1322. }
  1323. #ifndef unix
  1324. void wprintf(va_list arg_list, ...)
  1325. /* Prints to the window */
  1326. {
  1327.         va_list arg_ptr;
  1328.         char *format;
  1329.         char output[200];
  1330.  
  1331.         va_start(arg_ptr, arg_list);
  1332.         format = arg_list;
  1333.         vsprintf(output, format, arg_ptr);
  1334. #ifdef __TURBOC__
  1335.         printf(output);
  1336. #else
  1337.         cputs(output);
  1338. #endif
  1339. }
  1340. #endif
  1341.